<?php

namespace App\Admin\Controllers;

use App\Models\Cashiers;
use App\Models\Store;
use App\Models\AdminUser;
use App\Models\StoreDetail;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Route;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class StoreController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '门店';
    protected $status = [
        'on'  => ['value' => 1, 'text' => '正常', 'color' => 'success'],
        'off' => ['value' => 2, 'text' => '修整', 'color' => 'danger'],
    ];
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Store());

        $grid->column('detail.name', __('Name'));
        $grid->column('username', __('Username'));
        $grid->column('detail.sharing_percent', '分账百分比')->editable();
        $grid->column('detail.status', '状态')->switch($this->status);
        $grid->column('created_at', __('Created at'));

        // 筛选
        $grid->filter(function($filter){

            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            // 在这里添加字段过滤器
            $filter->like('name', __('Name'));
            
        });
        
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Store::findOrFail($id));

        $show->field('id', __('Id'));
//        $show->field('detail.name', __('Name'));
        $show->field('username', __('Username'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Store());

	// 判断是否为更新
	if(Route::currentRouteName() === 'stores.update' ||  Route::currentRouteName() === 'stores.edit') {
		$form->text('detail.name', '门店名称')
            ->rules('required|unique:stores,username,{{id}}');
		$form->text('username', __('Username'))->disable();
        $form->rate('detail.sharing_percent', '分账百分比')->default(0)
            ->rules('required|numeric|min:0|max:100');
        
        $form->password('password', __('Password'))->rules('required|confirmed');
        $form->password('password_confirmation', '确认密码')->rules('required')
	        ->default(function ($form) {
                return $form->model()->password;
            });
        $form->ignore(['password_confirmation']);
        
	    $form->switch('detail.status', '状态')->states($this->status);
	    $form->text('detail.sn', __('Sn'))->required();
        $form->text('detail.merchant_id', __('Merchant Id'))->required();
        
        $form->saving(function (Form $form) {
            if ($form->password && $form->model()->password != $form->password) {
                $form->password = bcrypt($form->password);
                $admin = AdminUser::where('store_id', $form->model()->id)
	                ->where('username', $form->model()->username)
	                ->first();
                $admin->password = $form->password;
                $admin->save();
            }
        });
	    
	    return $form;
	}
        $form->text('detail.name', __('Name'))
            ->rules('required|unique:store_details,name,{{id}}');
        $form->text('username', __('Username'))
	        ->rules('required|unique:stores,username,{{id}}');;
        
        $form->password('password', __('Password'))
            ->rules('required');
        $form->text('detail.sn', __('Sn'));
        $form->text('detail.merchant_id', __('Merchant Id'));

        $form->radio('detail.status', '状态')->options(['1' => '正常', '2' => '修整'])
            ->default(1);
        $form->rate('detail.sharing_percent', '分账百分比')->default(0)
        ->rules('required|numeric|min:0|max:100');
        $form->ignore(['detail.status']);
        
        if(Route::currentRouteName() !== 'stores.update') {
        	$form->saved(function (Form $form){
	            $status = request('detail.status');
	            if ($status == "off"){
	                $status = 2;
	            }else{
	                $status = 1;
	            }
	            // dd($form->detail['name']);
	            $store = Store::find($form->model()->id);
	            $store->password = Hash::make($form->password);
	            $store->name = $form->model()->username;
	            $store->save();
	            // 添加一个门店管理员账号
	            $admin = new AdminUser();
	            $admin->name = $form->model()->username;
	            $admin->username = $form->model()->username;
	            $admin->store_id = $form->model()->id;
	            $admin->password = Hash::make($form->password);
	            $admin->save();
	            $admin->roles()->attach([3]);// 店长
	            $admin->storeRoleUser()->attach([1,3]); //分店管理员
	        });
        }
        

        return $form;
    }
}
